package com.hevery.web.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.Date;

/**
 * @Author: Hevery
 * @Description:
 * @Date: Created on 2017/10/8
 * @Modified By:
 */

/**定义切片类*/
//@Aspect
/**将切片类注入到Spring容器中*/
//@Component
public class TimeAspect {

    /**
     * 其中@Around注解定义了在什么时候起作用；execution表达式定义了对那些方法起作用
     */
    @Around("execution(* com.hevery.web.controller.UserController.*(..))")
    /**定义增强，ProceedingJoinPoint表示当前拦截住的方法对象*/
    public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("time aspect start ");
        Object[] args = pjp.getArgs();
        for (Object arg : args) {
            System.out.println("arg is" + arg);
        }
        long start = new Date().getTime();
        /**相当于调用控制器中被拦截的方法*/
        Object obj = pjp.proceed();
        System.out.println("time aspect 耗时：" + (new Date().getTime() - start));
        System.out.println("返回值：" + obj.toString());
        System.out.println("time aspect end ");
        return obj;
    }
}